package com.crest.wm;


import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: prabu
 * Date: 22/4/13
 * Time: 4:05 PM
 * To change this template use File | Settings | File Templates.
 */
public class WatermarkService {


    Map<Ticket,Document> cache = new HashMap<>();






    public WatermarkService() {

    }


    public Ticket watermarkDocument(Document document) {


        //Initiate the process of watermarking document
        //doWatermark();
        WatermarkProcess watermarkProcess = new WatermarkProcess();
        watermarkProcess.setDocument(document);
        Thread thread = new Thread(watermarkProcess);
        thread.start();


        //

        //TODO
        //status = Status.STARTED;


        Ticket ticket = Ticket.newInstance();
        ticket.setId(new Random().nextInt());
        cache.put(ticket,document);
        return ticket;

    }





    public Status getStatus(Ticket ticket) {

        // do we really need to use the ticket object,..
        Document doc = cache.get(ticket);


        if (doc.getWatermark().isEmpty())
        {
            return Status.STARTED;
        }
        else
        {
            return Status.FINISHED;
        }



    }

    public static void main(String args[])
    {
        Book book = new Book();
        book.setAuthor("Murughan Joghee");
        book.setTitle("Zaplet ZDK");
        book.setTopic("who knows");


        WatermarkService watermarkService = new WatermarkService();
        Ticket ticket = watermarkService.watermarkDocument(book);
        System.out.printf(" the value of book ticket %d",ticket.getId());


        Journal journal = new Journal();
        journal.setTitle("Anirudha");
        journal.setAuthor("Anurathi");


        Ticket journalTkt = watermarkService.watermarkDocument(journal);
        Status journalStatus = watermarkService.getStatus(journalTkt);



        System.out.printf(" the value of journal ticket %d",journalTkt.getId());
        Status status = watermarkService.getStatus(ticket);

        for (int i=0; i<=17; i++)
        {
          System.out.printf(" the value of status %s\n",watermarkService.getStatus(ticket));
            System.out.printf(" the value of journal status %s\n",watermarkService.getStatus(journalTkt));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }

        //status = watermarkService.getStatus(ticket);







    }


}
